<?php

namespace App\Http\Controllers\Admin\Login;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Common\CommonApi;
use App\Model\AdminUsers;

class LoginController extends Controller
{   
    /**
     * 检验二维码
     * @param  Request $request [description]
     * @return [string]         正确返回字符串true, 否则返回false
     */
    public function checkCode(Request $request)
    {   

        $api = new CommonApi;
        $bool = $api->CheckCode($request, 'code');

        if ($bool) {
            return 'true';
        } else {
            return 'false';
        }
    }

    /**
     * 执行登陆
     * @param  Request $request [description]
     * @return mixed
     */
    public function login(Request $request)
    {
        $name = $request->input('name');
        $password = $request->input('password');

        $user = AdminUsers::where('name', $name)->first();

        if (!$user) {
            return back()->with([
                'errorCode' => '401',
                'errorTip' => '用户名不存在',
                'ex_name' => $name,
                'ex_password' => $password,
                ]);
        }

        if (password_verify($user->password, $password)) {
            return back()->with([
                'errorCode' => '402',
                'errorTip' => '密码不正确',
                'ex_name' => $name,
                'ex_password' => $password,
                ]);
        }

        $request->session()->put('adminuser.name', $user->name);
        $request->session()->put('adminuser.cover', $user->cover);

        return Redirect('admin');
        
    }

    /**
     * 执行登出
     * @param  Request $request 
     * @return 后台登陆页面
     */
    public function logout(Request $request)
    {
        $request->session()->forget('adminuser.name');
        $request->session()->forget('adminuser.cover');

        return Redirect('login');
    }



}
